﻿using System;
using System.Drawing;
using System.Windows.Forms;
using OpenGL;

namespace ConsoleApplication1
{
    static public class Program
    {
        static Form frm = null;
        static private CustomGLControl control = null;

        static private int ticks = Environment.TickCount;
        static private int frames = 0;

        static public void Main(string[] args)
        {
            control = new CustomGLControl();

            frm = new Form();
            control.KeyDown += frm_KeyDown;
            frm.FormClosed += frm_FormClosed;
            frm.SizeChanged += frm_SizeChanged;
            frm.Controls.Add(control);
            frm.ClientSize = new Size(800, 600);

            control.ContextMenu = new ContextMenu();
            control.ContextMenu.MenuItems.Add(new MenuItem("Show/hide facet", TriggerShowFacetMenuItemClicked));
            control.ContextMenu.MenuItems.Add(new MenuItem("Change shader", frm_KeyDown));

            frm.Show();

            control.InitOpenGL();

            MessagePump.Run(frm.Handle, Render);
        }

        static void TriggerShowFacetMenuItemClicked(object sender, EventArgs e)
        {
            control.TriggerFacet();
        }
        static void frm_KeyDown(object sender, EventArgs e)
        {
            if (e is KeyEventArgs)
            {
                if (((KeyEventArgs)e).KeyCode != Keys.Space)
                {
                    return;
                }
            }

            control.ChangeShader();
        }

        static private void Render()
        {
            control.Render();

            CountFPS();
        }
        static private void CountFPS()
        {
            frames++;
            if (Environment.TickCount - ticks >= 1000)
            {
                string text = string.Format("Garbage collections: {0} {1} {2} fps: {3}. Right click for context menu.",
                    GC.CollectionCount(0),
                    GC.CollectionCount(1),
                    GC.CollectionCount(2),
                    frames);

                frm.Text = text;
                ticks = Environment.TickCount;
                frames = 0;
            }
        }

        static private void frm_SizeChanged(object sender, EventArgs e)
        {
            control.Size = ((Form)sender).ClientSize;
        }
        static private void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MessagePump.Stop();
        }
    }
}
